package com.fone.player.airone;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;

import com.fone.player.util.L;

public class FBroadcast {
	private static final String TAG = "FBroadcast";
	
	public static void start() throws NumberFormatException, IOException{
		
		InetAddress receiveAddress = InetAddress.getByName("224.0.0.251");
		if( !receiveAddress.isMulticastAddress() ){
			new UnknownHostException("not Multicast Address");
		}
		MulticastSocket multi = new MulticastSocket(Integer.parseInt(System.getProperty("net.mdns.port","5353")));  
		multi.setTimeToLive(255);
		multi.joinGroup(receiveAddress);
		
		while(true){
			byte[] datas = new byte[1024*8];
			DatagramPacket dp = new DatagramPacket(datas,datas.length);   
			multi.receive(dp);
			InetAddress ia = dp.getAddress();
			L.i(TAG, ia.getHostAddress() +"--"+ia.getHostName() +" data:"+ new String(dp.getData(),"utf-8").trim());
//			System.out.println(ia.getHostAddress() +"--"+ia.getHostName() +" data:"+ new String(dp.getData(),"utf-8").trim());   
		}
	}
}
